<template>
  <div class="container">
<!--    <div style="width: 500px;">-->
<!--      <img src="@/assets/imgs/bg.jpg" alt="" style="width: 100%; margin-top: -200px">-->
<!--    </div>-->
    <div style="flex: 1; background-color: #f8f8f8">
      <div style="height: 50px; display: flex; align-items: center; background-color: white; padding-left: 20px">
        <img src="@/assets/imgs/logo1.png" alt="" style="width: 40px">
        <span style="font-size: 22px; margin-left: 5px">EcoSphere注册</span>
      </div>
      <div style="height: calc(100vh - 50px); display: flex; align-items: center; justify-content: center">
        <div style="width: 400px; padding: 40px; background-color: white; border-radius: 5px;">
          <div style="display: flex; margin-bottom: 50px; font-size: 24px">
            <a style="color: #333" href="/login"><div>登录</div></a>
            <div style="margin-left: 20px; border-bottom: 2px solid orangered; padding-bottom: 10px">注册</div>
          </div>
          <el-form :model="form" :rules="rules" ref="formRef">
            <el-form-item prop="username">
              <el-input size="medium" prefix-icon="el-icon-user" placeholder="*请输入账号" v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input size="medium" prefix-icon="el-icon-lock" placeholder="*请输入密码" show-password  v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input size="medium" prefix-icon="el-icon-lock" placeholder="*请确认密码" show-password  v-model="form.confirmPassword"></el-input>
            </el-form-item>
            <el-form-item prop="gender">
              <el-select v-model="form.sex" placeholder="请选择性别" @change="handleGenderChange" style="width: 100%" >
                <el-option label="男" :value="1"></el-option>
                <el-option label="女" :value="2"></el-option>
                <el-option label="未知" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="nickname">
              <el-input size="medium" prefix-icon="el-icon-chicken" placeholder="请输入昵称"  v-model="form.nickname"></el-input>
            </el-form-item>
            

            <div style="display: flex">
              <el-form-item prop="identifyCode" style="flex: 1;min-width: 115px;max-width: 167px ">
                <el-input prefix-icon="el-icon-circle-check" v-model="form.identifyCode" property="identifyCode"></el-input>
              </el-form-item>
              <div style="flex: 1;height: 36px;align-content: center" @click="refreshCode">
                <!--            验证码组件-->
                <SIdentify :identify-code="identifyCode"></SIdentify>
              </div>
            </div>

            <el-form-item>
              <el-button size="medium" class="btu1" @click="login">注 册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </div>


  </div>
</template>

<script>
import SIdentify from "@/components/SIdentify.vue";
export default {
  name: "Register",
  components:{SIdentify},
  data() {
    // 密码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    //验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'));
      } else {
        if (value!== this.identifyCode) {
          callback(new Error("验证码错误"))
        }else {
          callback();
        }

      }
    };
    
    return {
      form: { sex: '' ,identifyCode: "",status:0},
      gender: '',
      identifyCode: "",//验证码组件传过来的code
      identifyCodes: "1234567890abcdefghijklmnopqrst",
      dialogFormVisible:false,
      formLabelWidth: '120px',
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPassword: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        identifyCode:[
          {validator:validateCode, trigger: 'blur'}
        ],
        // gender:[
        //   {required: true, message: '请填写性别', trigger: 'blur'}
        // ]
      }
    }
  },
  created() {
    this.makeCode(this.identifyCodes, 4)
  },
  methods: {
    handleGenderChange(selectedValue) {
      // 将字符串转换为整数
      const genderValue = parseInt(selectedValue, 10);
      // 使用genderValue进行后续处理
      console.log(genderValue);  // 输出整数
      this.gender = genderValue;  // 如果需要的话，存储为整数
    },
    login() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/eco/user/add', this.form).then(res => {
            if (res.code === '200') {
              this.$router.push('/login')
              console.log(this.form)
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    randomNum(min, max) {
      max = max + 1;
      return Math.floor(Math.random() * (max - min) + min);
    },
    makeCode(data, len) {
      for (let i = 0; i < len; i++) {
        this.identifyCode += data[this.randomNum(0, data.length - 1)]
      }
      console.log("随机验证码" + this.identifyCode)
    }
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  display: flex;
}
a {
  color: #2a60c9;
}
.btu1 {
  width: 100%;
  background-color: green;
  border-color: darkolivegreen;
  color: white;
}
</style>